package com.demo2do.fms.admin.dao.supplier;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.fms.entity.driver.Driver;
import com.demo2do.fms.entity.driver.DriverAuditStatus;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 * 
 * @author Ysy
 *
 */
@Dao("hibernate")
public interface DriverDao {
    
    /**
     * 
     * @return
     */
    @Query("SELECT COUNT(*) FROM Driver WHERE disabled = false")
    public int count();
    
    /**
     * @param status
     * @return
     */
    @Query("SELECT COUNT(*) FROM Driver WHERE disabled = false AND status = :status")
    public int count(@Variable("status") DriverAuditStatus status);
    
    /**
     * @param supplierId
     * @return
     */
    @Query("SELECT COUNT(*) FROM Driver WHERE disabled = false AND supplier.id = :supplierId")
    public int count(@Variable("supplierId") Long supplierId);
    
    /**
     * @param status
     * @param bind
     * @return
     */
    @Query("SELECT COUNT(*) FROM Driver WHERE status = :status AND bind = :bind AND disabled = false")
    public int count(@Variable("status") DriverAuditStatus status, @Variable("bind") boolean bind);
    
    /**
     * @param supplierId
     * @param status
     * @param bind
     * @return
     */
    @Query("SELECT COUNT(*) FROM Driver WHERE supplier.id = :supplierId AND status = :status AND bind = :bind AND disabled = false")
    public int count(@Variable("supplierId") Long supplierId, @Variable("status") DriverAuditStatus status, @Variable("bind") boolean bind);

    /**
     * @param supplierId
     * @param status
     * @return
     */
    @Query("SELECT COUNT(*) FROM Driver WHERE disabled = false AND supplier.id = :supplierId AND status = :status")
    public int count(@Variable("supplierId") Long supplierId, @Variable("status") DriverAuditStatus status);
    
    /**
     * 
     * @param supplierIds
     * @return
     */
    @Query("SELECT COUNT(*) FROM Driver WHERE disabled = false AND supplier.id IN (:supplierIds)")
    public int count(@Variable("supplierIds") List<Long> supplierIds);
    
    /**
     * 
     * @param supplierIds
     * @param status
     * @return
     */
    @Query("SELECT COUNT(*) FROM Driver WHERE disabled = false AND supplier.id in (:supplierIds) AND status = :status")
    public int count(@Variable("supplierIds") List<Long> supplierIds, @Variable("status") DriverAuditStatus status);
    
    /**
     * 
     * @return
     */
    @Query("FROM Driver WHERE disabled = false")
    public List<Driver> list();
    
    /**
     * 
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Driver WHERE disabled = false")
    public List<Driver> list(@Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param status
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Driver WHERE disabled = false AND status = :status")
    public List<Driver> list(@Variable("status") DriverAuditStatus status, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param supplier
     * @return
     */
    @Query("FROM Driver WHERE supplier = :supplier AND disabled = false")
    public List<Driver> list(@Variable("supplier") Supplier supplier);
    
    /**
     * 
     * @param supplierId
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Driver WHERE disabled = false AND supplier.id = :supplierId")
    public List<Driver> list(@Variable("supplierId") Long supplierId, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param status
     * @param bind
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Driver WHERE status = :status AND  bind = :bind AND disabled = false")
    public List<Driver> list(@Variable("status") DriverAuditStatus status, @Variable("bind") boolean bind, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * 
     * @param supplierId
     * @param status
     * @param bind
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Driver WHERE supplier.id = :supplierId AND status = :status AND bind = :bind AND disabled = false")
    public List<Driver> list(@Variable("supplierId") Long supplierId, @Variable("status") DriverAuditStatus status, @Variable("bind") boolean bind, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param supplierId
     * @param status
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Driver WHERE disabled = false AND supplier.id = :supplierId AND status = :status")
    public List<Driver> list(@Variable("supplierId") Long supplierId, @Variable("status") DriverAuditStatus status, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * 
     * @param supplierId
     * @param status
     * @return
     */
    @Query("FROM Driver WHERE disabled = false AND supplier.id = :supplierId AND status = :status")
    public List<Driver> list(@Variable("supplierId") Long supplierId, @Variable("status") DriverAuditStatus status);
    
    /**
     * 
     * @param status
     * @return
     */
    @Query("FROM Driver WHERE disabled = false AND status = :status")
    public List<Driver> list(@Variable("status") DriverAuditStatus status);
    
    /**
     * @param supplierIds
     * @return
     */
    @Query("FROM Driver WHERE disabled = false AND supplier.id IN (:supplierIds)")
    public List<Driver> listBySupplierIds(@Variable("supplierIds") List<Long> supplierIds);
    
    /**
     * 
     * @param supplierIds
     * @param status
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Driver WHERE disabled = false AND supplier.id in (:supplierIds) AND status = :status")
    public List<Driver> list(@Variable("supplierIds") List<Long> supplierIds, @Variable("status") DriverAuditStatus status, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * 
     * @param supplierIds
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Driver WHERE disabled = false AND supplier.id IN (:supplierIds) ORDER BY status")
    public List<Driver> list(@Variable("supplierIds") List<Long> supplierIds, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param vehicleId
     * @return
     */
    @Query("FROM Driver WHERE disabled = false AND vehicleId = :vehicleId")
    public List<Driver> listByVehicle(@Variable("vehicleId") Long vehicleId);
    
    /**
     * 
     * @param vehicleId
     * @param status
     * @return
     */
    @Query("FROM Driver WHERE disabled = false AND vehicleId = :vehicleId AND status = :status")
    public List<Driver> listByVehicle(@Variable("vehicleId") Long vehicleId, @Variable("status") DriverAuditStatus status);
    
    /**
     * 
     * @param id
     * @return
     */
    @Query("FROM Driver WHERE id = :id AND disabled = false")
    public Driver load(@Variable("id") Long id);
    
    /**
     * @param code
     * @return
     */
    @Query("SELECT COUNT(*) FROM Driver WHERE code = :code AND disabled = false")
    public int countByCode(@Variable("code") String code);
    
    /**
     * @param code
     * @return
     */
    @Query("FROM Driver WHERE code = :code AND disabled = false")
    public Driver loadByCode(@Variable("code") String code);
    
    /**
     * @param citizenCode
     * @return
     */
    @Query("SELECT COUNT(*) FROM Driver WHERE citizenCode = :citizenCode AND disabled = false")
    public int countByCitizen(@Variable("citizenCode") String citizenCode);
    
    /**
     * @param citizenCode
     * @return
     */
    @Query("FROM Driver WHERE UPPER(citizenCode) = :citizenCode AND disabled = false")
    public Driver loadByCitizen(@Variable("citizenCode") String citizenCode);
    
    /**
     * @param mobile
     * @return
     */
    @Query("SELECT COUNT(*) FROM Driver WHERE mp = :mp AND disabled = false")
    public int countByMobile(@Variable("mp") String mobile);
    
    /**
     * @param mobile
     * @return
     */
    @Query("FROM Driver WHERE mp = :mp AND disabled = false")
    public Driver loadByMobile(@Variable("mp") String mobile);
    
    /**
     * @param citizenCode
     * @return
     */
    @Query("FROM Driver WHERE citizenCode = :citizenCode AND disabled = true")
    public Driver loadQuitDriver(@Variable("citizenCode") String citizenCode);
    
    /**
     * @param code
     * @param status
     * @return
     */
    @Query("FROM Driver WHERE (code = :code OR name = :code) AND status = :status AND disabled = false")
    public Driver load(@Variable("code") String code, @Variable("status") DriverAuditStatus status);
    
    /**
     * 
     * @param code
     * @param supplierId
     * @param status
     * @return
     */
    @Query("FROM Driver WHERE (code = :code OR name = :code) AND supplier.id = :supplierId AND status = :status AND disabled = false")
    public Driver load(@Variable("code") String code, @Variable("supplierId") Long supplierId, @Variable("status") DriverAuditStatus status);
    
    /**
     * 
     * @param ids
     * @return
     */
    @Query("FROM Driver WHERE id IN ( :driverIds ) AND disabled = false")
    public List<Driver> list(@Variable("driverIds") List<Long> ids);
    
}
